package 博客管理系统;

import java.sql.*;
import javax.swing.JFrame;
import java.awt.event .*;
import javax.swing .*;

public class Comments extends JFrame {
    JTextField jtf=new JTextField(10);
    JTextArea jta=new JTextArea(6,40);

    public Comments() {
        JPanel p=new JPanel();
        JLabel lb=new JLabel("请输入管理员ID:");
        JButton b1=new JButton("查询评论信息");
        b1.addActionListener(new Abc());
        JButton b2=new JButton("关闭");
        b2.addActionListener(e->System.exit(0));

        p.add(lb);
        p.add(jtf);
        p.add(b1);
        p.add(b2);
        p.add(jta);
        getContentPane().add(p);
        setTitle("管理员权限");
        setSize(800,600);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public static void main (String[] args) {
        new  Comments();
    }
    class Abc implements ActionListener{
        public void actionPerformed(ActionEvent e){
            String xh=jtf.getText();
            Connection con;
            Statement st;
            ResultSet rs;
            String dbURL="jdbc:sqlserver://localhost:1433;DatabaseName=BlogSystemLD";
            String userName="sa";
            String userPwd="123";
            String sql="select * from Comments";
            try {
                con = DriverManager.getConnection(dbURL, userName, userPwd);
                st = con.createStatement();
                rs = st.executeQuery(sql);
                String adminstrators="王老师";
                if(xh.equals(adminstrators)){
                    while (rs.next()) {
                        String send_id = rs.getString(1);
                        String receive_id = rs.getString(2);
                        String content = rs.getString(3);
                        String create_time = rs.getString(4);
                        jta.append("评论者ID："+send_id+"\r\n");
                        jta.append("被评论者ID："+receive_id+"\r\n");
                        jta.append("评论内容："+content+"\r\n");
                        jta.append("评论时间："+create_time+"\r\n");
                    }
                    rs.close();
                    st.close();
                    con.close();
                }
                else{
                    JOptionPane.showMessageDialog(null,"非管理员禁止访问！！！");
                }
            }catch(SQLException exception){
                exception.printStackTrace();
                System.out.println("数据库连接失败！！！");
            }

        }
    }
}